package com.ruoyi.yys.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.yys.domain.ImportExportTemplate;
import com.ruoyi.yys.domain.entityBo.ImportExportParamBo;
import com.ruoyi.yys.service.IImportExportTaskService;
import com.ruoyi.yys.service.IImportExportTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 导入导出模板Controller
 * 
 * @author xinyin
 * @date 2022-12-07
 */
@RestController
@RequestMapping("/importExport/template")
@RequiredArgsConstructor
public class ImportExportTemplateController extends BaseController
{
    private final IImportExportTemplateService importExportTemplateService;

    private final IImportExportTaskService importExportTaskService;

    /**
     * 查询导入导出模板列表
     */
    @PreAuthorize("@ss.hasPermi('importExport:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImportExportTemplate importExportTemplate)
    {
        startPage();
        List<ImportExportTemplate> list = importExportTemplateService.selectImportExportTemplateList(importExportTemplate);
        return getDataTable(list);
    }

    /**
     * 导出导入导出模板列表
     */
    @PreAuthorize("@ss.hasPermi('importExport:template:export')")
    @Log(title = "导入导出模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImportExportTemplate importExportTemplate)
    {
        List<ImportExportTemplate> list = importExportTemplateService.selectImportExportTemplateList(importExportTemplate);
        ExcelUtil<ImportExportTemplate> util = new ExcelUtil<ImportExportTemplate>(ImportExportTemplate.class);
        util.exportExcel(response, list, "导入导出模板数据");
    }

    /**
     * 获取导入导出模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('importExport:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(importExportTemplateService.selectImportExportTemplateById(id));
    }

    /**
     * 新增导入导出模板
     */
    @PreAuthorize("@ss.hasPermi('importExport:template:add')")
    @Log(title = "导入导出模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImportExportTemplate importExportTemplate)
    {
        return toAjax(importExportTemplateService.insertImportExportTemplate(importExportTemplate));
    }

    /**
     * 上传excel模板文件
     */
    @PreAuthorize("@ss.hasPermi('importExport:template:add')")
    @Log(title = "上传excel模板文件", businessType = BusinessType.INSERT)
    @PostMapping("/uploadExcelTemplate")
    public AjaxResult uploadExcelTemplate(Long id,MultipartFile file)
    {
        return toAjax(importExportTemplateService.uploadExcelTemplate(id,file));
    }

    /**
     * 修改导入导出模板
     */
    @PreAuthorize("@ss.hasPermi('importExport:template:edit')")
    @Log(title = "导入导出模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImportExportTemplate importExportTemplate)
    {
        return toAjax(importExportTemplateService.updateImportExportTemplate(importExportTemplate));
    }

    /**
     * 删除导入导出模板
     */
    @PreAuthorize("@ss.hasPermi('importExport:template:remove')")
    @Log(title = "导入导出模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(importExportTemplateService.deleteImportExportTemplateByIds(ids));
    }

    /**
     * 将文件内容导入到数据库中
     *
     * @param file 文件
     */
    @PostMapping("/importByFile")
    public R importByFile(ImportExportParamBo paramBo, MultipartFile file) {
        paramBo.setFile(file);
        String id = importExportTaskService.importByFile(paramBo);
        return R.ok(id);
    }

    /**
     * downloda模板ossid文件
     *
     * @param id       id
     * @param fileName 设置下载后的文件名称
     * @param response 响应
     * @return {@link R}
     */
    @PostMapping("/downlodaTemplateOssFileById")
    public R downlodaTemplateOssFileById(Long id,String fileName, HttpServletResponse response) {
        return importExportTemplateService.downlodaTemplateOssFileById(id,fileName,response);
    }

    /**
     * 模板列表
     *
     * @return {@link R}
     */
    @GetMapping("/listTemplate")
    public R listTemplate() {
        return R.ok(importExportTemplateService.listTemplate());
    }
}
